package com.spukmk3me.util;

import java.util.LinkedList;

public class StringUtil
{
    public static int getIndexInArray( String[] array, String target )
    {
        for ( int i = 0; i != array.length; ++i )
        {
            if ( target.equals( array[ i ] ) )
                return i;
        }
        
        return -1;
    }
    
    public static String getBeforeNextSeparator( String s, char separator )
    {
        int sepIndex = s.indexOf( separator );
        
        if ( sepIndex == -1 )
            return s;
        
        return s.substring( 0, sepIndex );
    }
    
    public static String removeTillNextSeparator( String s, char separator )
    {
        int sepIndex = s.indexOf( separator );
        
        if ( sepIndex == -1 )
            return "";
        
        return s.substring( sepIndex + 1 );
    }
    
    public static float readNumber( String s, float defaultValue )
    {
        if ( s == null )
            return defaultValue;
        
        try
        {
            return Float.parseFloat( s );
        } catch ( NumberFormatException e ) {
            return defaultValue;
        }
    }
    
    public static int readIntNumber( String s, int defaultValue )
    {
        if ( s == null )
            return defaultValue;
        
        try
        {
            return Integer.parseInt( s );
        } catch ( NumberFormatException e ) {
            return defaultValue;
        }
    }
    
    public static float[] readNumberList( String s, char separator )
    {
        LinkedList<Float> list = new LinkedList<Float>();
        String subStr;
        
        while ( s.length() > 0 )
        {
            subStr = getBeforeNextSeparator( s, separator );
            s = removeTillNextSeparator( s, separator );
            list.add( Float.parseFloat( subStr ) );
        }
        
        float[] m = new float[ list.size() ];
        
        for ( int i = 0; i != m.length; ++i )
            m[ i ] = list.get( i );
        
        return m;
    }
    
    public static int[] readIntNumberList( String s, char separator )
    {
        float[] m1 = readNumberList( s, separator );
        int[] m2 = new int[ m1.length ];
        
        for ( int i = 0; i != m1.length; ++i )
            m2[ i ] = (int)m1[ i ];
        
        return m2;
    }
    
    /**
     *  Convert from absolute path to relative path.
     *  @param absoluteFilePath The absolute path to be converted.
     *  @param absoluteBasePath The absolute path of directory to create
     * relative path to file path.
     *  @param sourcePlatformPathSeparator Path separator used in
     * absoluteFilePath and absoluteBasePath.
     *  @param destPlatformPathSeparator Path separator used in result.
     *  @result Relative path from base path to file path.
     */
    public static String convertToRelativePath(
        String absoluteFilePath, String absoluteBasePath,
        char sourcePlatformPathSeparator, char destPlatformPathSeparator )
    {
        int fileLength  = absoluteFilePath.length();
        int saveLength  = absoluteBasePath.length();

        if ( (fileLength == 0) || (saveLength == 0) )
            return null;

        char[] _absoluteFilePath = absoluteFilePath.toCharArray();
        char[] _absoluteBasePath = absoluteBasePath.toCharArray();

        for( int i = 0; i != saveLength; ++i )
        {
            if ( _absoluteBasePath[ i ] == sourcePlatformPathSeparator )
                _absoluteBasePath[ i ] = destPlatformPathSeparator;
        }

        for( int i = 0; i != fileLength; ++i )
        {
            if ( _absoluteFilePath[ i ] == sourcePlatformPathSeparator )
                _absoluteFilePath[ i ] = destPlatformPathSeparator;
        }

        absoluteFilePath = new String( _absoluteFilePath );
        absoluteBasePath = new String( _absoluteBasePath );

        // Search for the first separator
        int i = 0;

        while ( _absoluteFilePath[ i ] != destPlatformPathSeparator )
        {
            ++i;

            if ( (i == fileLength) || (i == saveLength) )
                return null;
        }
        // Done searching for first separator

        if ( !absoluteFilePath.regionMatches( false,
            0, absoluteBasePath, 0, i + 1 ) )
            return null;

        int lastMatchedSeparatorIndex = i;
        
        ++i;

        while ( (i != fileLength) && (i != saveLength) )
        {
            if ( _absoluteFilePath[ i ] != _absoluteBasePath[ i ] )
                break;

            if ( _absoluteFilePath[ i ] == destPlatformPathSeparator )
                lastMatchedSeparatorIndex = i;

            ++i;
        }

        int nUpperDir = 0;

        for ( i = lastMatchedSeparatorIndex; i != saveLength; ++i )
        {
            if ( _absoluteBasePath[ i ] == destPlatformPathSeparator )
                ++nUpperDir;
        }

        String resultPath = "";
        String parentPath = ".." + destPlatformPathSeparator;

        for ( i = nUpperDir; i != 0; --i )
            resultPath += parentPath;

        resultPath += absoluteFilePath.substring(
            lastMatchedSeparatorIndex + 1 );
        return resultPath;
    }

    /**
     *  Convert from relative path to absolute path.
     *  @param basePath The base directory.
     *  @param relativePath Relative path to base directory.
     *  @param srcPathSeparator Path separator used in basePath and
     * relativePath.
     *  @param destPathSeparator Path separator used in result.
     *  @return 
     */
    public static String convertToAbsolutePath( String basePath,
        String relativePath, char srcPathSeparator, char destPathSeparator )
    {
        char[] _basePath        = basePath.toCharArray();
        char[] _relativePath    = relativePath.toCharArray();

        for ( int i = 0; i != _basePath.length; ++i )
        {
            if ( _basePath[ i ] == srcPathSeparator )
                _basePath[ i ] = destPathSeparator;
        }

        for ( int i = 0; i != _relativePath.length; ++i )
        {
            if ( _relativePath[ i ] == srcPathSeparator )
                _relativePath[ i ] = destPathSeparator;
        }

        relativePath     = new String( _relativePath );
        basePath   = new String( _basePath );

        String parentPath = ".." + destPathSeparator;
        int lastParentIndex = -1, nParent = 0, temp;

        while ( (temp = relativePath.indexOf( parentPath, lastParentIndex + 1 ))
            != -1 )
        {
            lastParentIndex = temp + 2;
            ++nParent;
        }

        int sceneTruncatedIndex = basePath.length() - 1;

        for ( int i = nParent; i != 0; --i )
        {
            sceneTruncatedIndex = basePath.lastIndexOf(
                destPathSeparator, sceneTruncatedIndex ) - 1;
        }
        
        // Special case: path = ""
        String path1 = basePath.substring( 0, sceneTruncatedIndex + 1 );
        String path2 = relativePath.substring( lastParentIndex + 1 );
        
        if ( path1.length() == 0 )
            return path2;
        else
            return path1 + destPathSeparator + path2;
    }

}
